<?php

namespace app\apiz\controller\product;

use think\Controller;
use think\Cookie;

class Product extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->TypeModel = model('Product.Type');
        $this->ProductModel = model('Product.Product');
        $this->UserModel = model('User.User');
        $this->VipModel = model('Product.Vip');
        $this->SwiperModel = model('Product.Swiper');
        $this->CountdownModel = model('Product.Countdown');
        $this->CollectModel = model('Product.Collect');




    }
    // 首页商品
    public function index()
    {
        $type = $this->TypeModel->limit(8)->select();
        //查询新品
        $news = $this->ProductModel
                        ->with('type')
                        ->where(['flag'=>1])
                        ->order('id','desc')
                        ->limit(3)
                        ->select();

        $recom = $this->ProductModel
                        ->with('type')
                        ->where(['flag'=>3])
                        ->order('id')
                        ->limit(4)
                        ->select();

        //热销
        $hots = $this->ProductModel
                        ->with('type')
                        ->where(['flag'=>2])
                        ->order('id','desc')
                        ->limit(6)
                        ->select();

        $swiper = $this->SwiperModel->limit(5)->select();



       
        $data = [
            'swiper' => $swiper,
            'type' => $type,
            'news' => $news,
            'hots' => $hots,
            'recom' => $recom
        ];

        $this->success('返回首页数据', null, $data);
        exit;
                        
    }

    public function countdown()
    {
        if($this->request->isPost())
        {

        }
    }
    // 分类列表
    public function type()
    {
        if($this->request->isPost())
        {
            $Type = $this->TypeModel->limit(8)->select();
            
            $data = [
                'Type' => $Type
            ];

            $this->success('返回首页数据', null, $data);
            exit;
        }
    }
    // 搜索页面
    public function search()
    {
        if($this->request->isPost())
        {        
            $flag = $this->request->param('flag', 0);
            $orderby = $this->request->param('orderby', 'id');
            $keywords = $this->request->param('keywords', '');
            $where = [];

            if($flag)
            {
                $where['flag'] = $flag;
            }

            if(!empty($keywords))
            {                             //模糊匹配
                $where['name'] = ['like',"%$keywords%"];
            }

            //获取商品列表
            $prolist = $this->ProductModel->where($where)->order($orderby, 'desc')->select();

            $this->success('查询商品数据成功', null, $prolist);
            exit;

        }
    }
    // 输入框
    public function word()
    {
        if($this->request->isPost())
        {
            $typeid = $this->request->param('typeid', 0);

            $type = $this->TypeModel->where('weigh',$typeid)->find();
            
            $this->success('查询分类成功', null, $type);
            exit;
        }
    }

    public function class()
    {
        if($this->request->isPost())
        {
            $typeid = $this->request->param('typeid', 0);
            
            $prolist = $this->ProductModel->with('type')->where('typeid',$typeid)->order('id','desc')->select();

            $this->success('查询商品数据成功', null, $prolist);
            exit;
        }
    }

    //商品详情
    public function info()
    {
        if($this->request->isPost())
        {
            //获取商品的id
            $proid = $this->request->param('proid', 0);
            $userid = $this->request->param('userid', 0);

            //查询商品的详细
            $product = $this->ProductModel->with(['type'])->find($proid);

            if(!$product)
            {
                $this->error('商品不存在');
                exit;
            }

            $user = $this->UserModel->find($userid);
            $vip = isset($user) ? $user['vip'] : 0;

            if($vip > 0)
            {
                $level = $this->VipModel->where(['proid' => $proid, 'level' => $vip])->value('price');
                $product['level'] = $level;
            }

            $this->success('返回商品详情', null, $product);
            exit;
        }

    }

    //添加收藏
    public function collectadd()
    {
        if($this->request->isPost())
        {
            
            $userid = $this->request->param('userid', 0);
            $proid = $this->request->param('proid', 0);

            $User = $this->UserModel->find($userid);
            if(!$User)
            {
                $this->error('用户不存在');
                exit;
            }
            $product = $this->ProductModel->find($proid);
            if(!$product)
            {
                $this->error('商品不存在');
                exit;
            }
            $where =[
                'userid' => $userid,
                'proid' => $proid
            ];
            // 判断是否已经添加过商品
            $collect = $this->CollectModel->where($where)->find();

            if($collect)
            {
                $this->success('您已收藏该商品无需重复添加收藏');
                exit;
            }else
            {
                $data = [
                    'userid' => $userid,
                    'proid' => $proid,
                ];

                $result = $this->CollectModel->save($data);
                if($result == FALSE)
                {
                    $this->error('添加收藏失败');
                    exit;
                }else
                {
                    $this->success('添加收藏成功,可前往收藏查看');
                    exit;
                }
            }

        }
    }

    //获取收藏数据
    public function collectindex()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid', 0);

            $User = $this->UserModel->find($userid);

            if(!$User)
            {
                $this->error('用户不存在');
                exit;
            }
            $collect = $this->CollectModel->with(['product'])->where('userid',$userid)->select();

            $this->success('收藏商品返回成功',null,$collect);
            exit;
        }
    }

    public function collectdel()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid', 0);
            $collectid = $this->request->param('collectid', 0);

            $where = [
                'id' => $collectid,
                'userid' => $userid
            ];

            $collect = $this->CollectModel->where($where)->find();

            if(!$collect)
            {
                $this->error('收藏记录不存在');
                exit;
            }

            //删除记录
            $result = $this->CollectModel->destroy($collectid);
            
            if($result === FALSE)
            {
                $this->error($this->CollectModel->getError());
                exit;
            }else
            {
                $this->success('取消收藏成功');
                exit;
            }
        }
    }

    public function collectstatu()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid', 0);
            $proid = $this->request->param('proid', 0);

            $User = $this->UserModel->find($userid);
            if(!$User)
            {
                $this->error('用户不存在');
                exit;
            }
            $product = $this->ProductModel->find($proid);
            if(!$product)
            {
                $this->error('商品不存在');
                exit;
            }
            $where = [
                'userid' => $userid,
                'proid' => $proid
            ];

            $result = $this->CollectModel->where($where)->select();
            
            if($result == FALSE)
            {
                $this->error('暂无记录');
                exit;
            }else
            {
                $this->success('已收藏');
                exit;
            }
        }
    }
}